<?php
/**
 * Pnm.php
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to version 2.0 of the Apache license
 * that is available through the world-wide-web at the following URI:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * @category Controller
 * @package  Controller
 * @author   Alex Kopp <koppa@rpi.edu>
 * @license  http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link     http://http://code.google.com/p/tri-chi/
 */

/**
 * Pnm
 *
 * The model that manages Potential New Members
 *
 * The followings are the available columns in table 'pnm':
 * 
 * @property string $id
 * @property string $first_name
 * @property string $last_name
 * @property string $picture_id
 * @property string $phone_number
 * @property string $email
 * @property string $major
 * @property string $dorm
 *
 * The followings are the available model relations:
 * @property Attendedevent[] $attendedevents
 * @property Comments[] $comments
 * @property Customfieldvalues[] $customfieldvalues
 * @property Introductions[] $introductions
 * @property PnmPictures $picture
 * @property Ratings[] $ratings
 *
 * @category Controller
 * @package  Controller
 * @author   Alex Kopp <koppa@rpi.edu>
 * @license  http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
 * @link     http://http://code.google.com/p/tri-chi/
 */
class Pnm extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * 
     * @param unknown_type $className Primary Key
     * 
     * @return Pnm the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * Return Table Name
     * 
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'pnm';
    }

    /**
     * Validation Rules
     * 
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: we should only define rules for those attributes that
        // will receive user inputs.
        return array(
        array('first_name, last_name', 'required'),
        array('first_name, last_name', 'length', 'max'=>32),
        array('dorm', 'length', 'max'=>64),
        array('email', 'length', 'max'=>64),
        array('email','email'),
        array(
            'picture_id',
            'file',
            'types'=>'jpg, gif, png',
            'allowEmpty' => 'true'
        ),
        array('phone_number, major, email', 'safe'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('first_name, last_name, dorm, phone_number, major, email', 'safe', 'on'=>'search'),
        );
    }

    /**
     * Table Relations
     * 
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'attendedevents' => array(self::HAS_MANY, 'Attendedevent', 'pnm_id'),
            'comments' => array(self::HAS_MANY, 'Comments', 'pnm_id'),
            'customfieldvalues' => array(
                self::HAS_MANY, 'Customfieldvalues', 'pnm_id'
            ),
            'introductions' => array(self::HAS_MANY, 'Introductions', 'pnm_id'),
            'picture' => array(self::BELONGS_TO, 'PnmPictures', 'picture_id'),
            'ratings' => array(self::HAS_MANY, 'Ratings', 'pnm_id'),
        );
    }

    /**
     * Attribute Labels
     * 
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'first_name' => 'First Name',
            'last_name' => 'Last Name',
            'picture_id' => 'Picture',
            'dorm' => 'Dorm',
            'major' => 'Major',
            'phone_number' => 'Phone Number',
            'email' => 'Email Address',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * 
     * @return CActiveDataProvider data provider that returns models based on search
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        //$criteria->compare('id',$this->id,true);
        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        //$criteria->compare('picture_id',$this->picture_id,true);
        $criteria->compare('dorm', $this->dorm, true);
        $criteria->compare('major', $this->major, true);
        $criteria->compare('phone_number', $this->phone_number, true);
        $criteria->compare('email', $this->email, true);

        return new CActiveDataProvider(
            $this, array(
                'criteria'=>$criteria,
            )
        );
    }
}